package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.adapter.MyListAdapter;
import com.example.myapplication.adapter.UserListView;
import com.example.myapplication.util.JsonUtil;
import com.example.myapplication.util.OkHttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserListViewActivity extends AppCompatActivity {
    private ListView listView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_listview);

        listView = findViewById(R.id.listView);
        userAll();


        TextView tv = findViewById(R.id.tv1);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setListView();
            }
        });



    }
    public void userAll(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtil.okGet("/userAll", new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Log.e("请求=========================", "失败");
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String res = response.body().string();
                        ObjectMapper objectMapper = new ObjectMapper();
//                        ArrayList<Map> listMap = objectMapper.readValue(res, new TypeReference<ArrayList<Map>>() {});
                        List<Map> listMap = JsonUtil.getListMap(res);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                listView.setAdapter(new UserListView(UserListViewActivity.this,listMap));
                                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        Intent intent = new Intent(UserListViewActivity.this,AlterUserActivity.class);
                                        try {
                                            String user = JsonUtil.getJson(listMap.get(position));
                                            intent.putExtra("user",user);
                                            startActivity(intent);
                                        } catch (JsonProcessingException e) {
                                            e.printStackTrace();
                                        }
//                                        Toast.makeText(UserListViewActivity.this,listMap.get(position).toString(),Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        });
                    }
                });

            }
        }).start();

    }
    //重新调用页面执行
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        userAll();
    }
    public void setListView(){
        try {
            HashMap map = new HashMap();
            map.put("id",1);
            map.put("name","suer1");
            map.put("age",18);
            String mapJson = JsonUtil.getJson(map);
            String string = "[{\"update_time\":\"2021-07-28\",\"create_time\":\"2021-07-28\",\"phone\":\"496162\",\"name\":\"user1\",\"id\":1,\"age\":10},{\"update_time\":\"2021-07-30\",\"create_time\":\"2021-07-30\",\"phone\":\"49652169565\",\"name\":\"user2\",\"id\":2,\"age\":11},{\"update_time\":\"2021-07-30\",\"create_time\":\"2021-07-30\",\"phone\":\"46195\",\"name\":\"user3\",\"id\":3,\"age\":25},{\"update_time\":\"2021-07-30\",\"create_time\":\"2021-07-30\",\"phone\":\"4656\",\"name\":\"suer4\",\"id\":4,\"age\":56},{\"update_time\":\"2021-07-30\",\"create_time\":\"2021-07-30\",\"phone\":\"4616\",\"name\":\"user5\",\"id\":5,\"age\":6}]";

//            List<Map> listMap = JsonUtil.getListMap(string);
            Log.d("listMap============================", mapJson);
        }catch (Exception e){
            Log.e("报错", "setListView: ", e);
        }

    }

}